#pragma once

#include "Log.hpp"
#include "comm.hpp"
#include "inetAddr.hpp"

using namespace LogModule;

namespace SocketModule
{
    // 基类套接字
    class Socket
    {
    public:
        virtual ~Socket() {}
        virtual void SocketOrDie() = 0;
        virtual bool BindOrDie(uint16_t port) = 0;
        virtual bool ListenOrDie(int backlog) = 0;
        // virtual std::shared_ptr<Socket> Accept(InetAddr *client) = 0;
        virtual int Accept(InetAddr *client) = 0;
        virtual void Close() = 0;
        virtual int Recv(std::string *out) = 0;
        virtual int Send(const std::string &message) = 0;
        virtual int Connect(const std::string ip, uint16_t port) = 0;
        virtual int Fd() = 0;
    public:
        void BuildTcpSocket(uint16_t port)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(5);
        }

        void BuildTcpClientSocket()
        {
            SocketOrDie();
        }

        // void BuildUdpSocket(uint16_t port)
        // {
        //     SocketOrDie();
        //     BindOrDie(port);
        // }
    };

    const int defaultfd = -1;
    class TcpSocket : public Socket
    {
    public:
        TcpSocket() :_sockfd(defaultfd) {}
        TcpSocket(int fd) :_sockfd(fd) {}
        ~TcpSocket() {}

        void SocketOrDie() override
        {
            _sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if(_sockfd < 0)
            {
                LOG(LogLevel::FATAL) << "socket error";
                exit(SOCKET_ERR);
            }
            int opt = 1;
            setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
            LOG(LogLevel::INFO) << "socket success, sockfd: " << _sockfd;
            return;
        }

        bool BindOrDie(uint16_t port) override
        {
            InetAddr addr(port);
            int n = ::bind(_sockfd, addr.NetAddrPtr(), addr.NetAddrLen());
            if(n < 0)
            {
                LOG(LogLevel::FATAL) << "bind error";
                exit(BIND_ERR);
            }
            LOG(LogLevel::INFO) << "bind success";
            return true;
        }

        bool ListenOrDie(int backlog) override
        {
            int n = ::listen(_sockfd, backlog);
            return true;
        }

        // std::shared_ptr<Socket> Accept(InetAddr *client) override
        int Accept(InetAddr *client) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int fd = accept(_sockfd, CONV(peer), &len);
            if(fd < 0)
            {
                LOG(LogLevel::WARNING) << "accept error";
                return -1;
            }
            client->SetAddr(peer);
            return fd;
        }

        void Close() override
        {
            if(_sockfd >= 0)
            {
                close(_sockfd);
            }
        }

        int Recv(std::string *out) override
        {
            char buff[4096*2];
            // ssize_t n = ::read(_sockfd, buff, sizeof(buff)-1);
            ssize_t n = ::recv(_sockfd, buff, sizeof(buff)-1, 0);
            if(n > 0)
            {
                buff[n] = 0;
                *out += buff;
            }
            return n;
        }
        int Send(const std::string &message) override
        {
            return ::send(_sockfd, message.c_str(), message.size(), 0);
        }

        int Connect(const std::string ip, uint16_t port) override
        {
            InetAddr server(ip, port);
            return ::connect(_sockfd, server.NetAddrPtr(), server.NetAddrLen());
        }

        int Fd() override
        {
            return _sockfd;
        }
        
    private:
        int _sockfd;
    };

    // class UdpSocket : public Socket
    // {
    //     ~UdpSocket() {}
    //     void SocketOrDie() override
    //     {
    //         return;
    //     }
    //     bool BindOrDie(uint16_t port) override
    //     {
    //         return true;
    //     }
    //     bool ListenOrDie(int backlog) override
    //     {
    //         return true;
    //     }
    //     std::shared_ptr<Socket> Accept(InetAddr *client) override
    //     {
    //         return nullptr;
    //     }
    // };
} 